﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
namespace Skynet.Office
{
    public class WordUtility 
    {
        public static  Word.Application CreateWordApplication(string param)
        {
            Word.Application word =  null;
             try
            {
                word = new Word.Application();
                word.Visible = true;
                Word.Documents docs = (Word.Documents)word.Documents;
                object templateName = Type.Missing;
                object openAsNewTemplate = Type.Missing;
                object openVisible = true;
                object documentType = Word.WdDocumentType.wdTypeDocument;
                Word.Document doc = docs.Add(ref templateName, ref openAsNewTemplate, ref documentType, ref openVisible);
                object startPosition = 0;
                object endPosition = 0;
                Word.Range r = (Word.Range)doc.Range(ref startPosition, ref endPosition);
                r.InsertAfter("Hello TestWordPIAForm");
            }
            catch (Exception ex)
            {
                 Console.WriteLine(ex.Message);
            }
             return word;
        }
        public static void ReleaseWordApplication(Word.Application word)
        {
            object missing = Type.Missing;
            word.Quit(ref missing, ref missing, ref missing);
            word = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();

        }
    }
}
